<?php

/*@LICENSE@*/

require_once 'Clouseau/Genealogy/Tree/Exporter.php';

/**
 * An exporter to GraphMl for a genealogy tree.
 * http://graphml.graphdrawing.org/primer/graphml-primer.html
 * @author Thomas Quémard <thomas.quemard@gmail.com>
 **/
class Clouseau_Genealogy_Tree_Exporter_GraphMl extends Clouseau_Genealogy_Tree_Exporter
{
	protected $_sEdges = '';
	protected $_sNodes = '';
	protected $_sTab = '   ';

	// @FIXME: Must use hyperedge for multiple edges, not valid right now.
	protected function _exportNode(Clouseau_Genealogy_Node &$oNode)
	{
		$s = '';
		foreach ($oNode as $oChild)
		{
			if (!is_null($oChild))
			{
				$this->_sNodes .= $this->_tab(2) . '<node id="' . $oChild->getName() . '" />' . "\n";
				$this->_sEdges .= $this->_tab(2) . '<edge source="' . $oNode->getName() . '" target="' . $oChild->getName() . "\" />\n";
				$this->_exportNode($oChild);
			}
		}
		return $s;
	}

	protected function _tab($i=1)
	{
		return str_repeat($this->_sTab, $i);
	}

	public function export(Clouseau_Genealogy &$oTree)
	{
		$this->_exportNode($oTree);

		$s = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
		$s .= '<graphml xmlns="http://graphml.graphdrawing.org/xmlns"';
		$s .= ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
		$s .= ' xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">' . "\n";
		$s .= $this->_sTab . '<graph id="G" edgedefault="undirected">' . "\n";
		$s .= $this->_sNodes;
		$s .= $this->_sEdges;
		$s .= $this->_sTab . "</graph>\n";
		$s .= '</graphml>';
		return $s;
	}
}
